using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;
using EventType = EventUtils.EventType;

namespace View.Instant
{
    public class LuckyView : View
    {
        private Button mbtnClose;
        private Text mtxtTimeOut;
        private Button mtxtNowGet;

        private bool iskaishi = false;
        
        public override void OnLoad()
        {
            base.OnLoad();

            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            mtxtTimeOut = transform.Find("timeout").GetComponent<Text>();
            mtxtNowGet = transform.Find("btnNowGet").GetComponent<Button>();

            AddClickListener(mbtnClose, () =>
            {
                UIManager.Instance.Close(ViewName.LuckyView);
            });
            AddClickListener(mtxtNowGet, () =>
            {
                //去看广告
                // GameHelp.G2SeeAdvAddTimes = 10;//增加步数 点击看完进游戏有效，推到外面再进来无效
                // GameHelp.G1BackTimes = 1;
                // GameLocalInfo.mSubTime = 120;

                string daojishi = GameLocalInfo.OnGetTimeStrByTimer();
                if (daojishi == "-1")
                {
                    OnGetGift();
                }
                else
                {
                    Debug.LogError("看广告去消除");
                    UIManager.Instance.OnShowLoading();
                    AndroidManager.Ins.LoadReward(() =>
                    {
                        UIManager.Instance.OnCloseLoading();
                        TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.adv);
                        EventCenter.BroadCast<string>(EventType.EVENT_SEE_ADV_END,ViewName.LuckyView);
                    });
                }

            });
        }

        private void OnUpdate(float timescale)
        {
            if (iskaishi)
            {
                string daojishi = GameLocalInfo.OnGetTimeStrByTimer();
                if (daojishi == "-1")
                {
                    //表示结束
                    mtxtTimeOut.text = "领取";
                    iskaishi = false;
                }
                else
                {
                    mtxtTimeOut.text = daojishi;
                }
            }
        }

        private void OnSeeAdvCallback(string str)
        {
            if (str == ViewName.LuckyView)
            {
                OnGetGift();
            }
        }

        private void OnGetGift()
        {
            GameLocalInfo.mLoginLuckyTimer = GameLocalInfo.OnGetNowTimeToLucky();

            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.fanpai));
            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.xiaochu));
            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.jiashi));
            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.jiashi));
            GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(PropItemEnum.xiaochu));
            EventCenter.BroadCast<List<PropItemEnum>>(EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){PropItemEnum.fanpai,PropItemEnum.xiaochu,PropItemEnum.jiashi,PropItemEnum.jiashi});
        }
        
        public override void OnShow(params object[] args)
        {
            base.OnShow();
            Utils.OnUpdate += OnUpdate;
            string daojishi = GameLocalInfo.OnGetTimeStrByTimer();
            iskaishi = daojishi == "-1" ? false : true;
            EventCenter.AddListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            Utils.OnUpdate -= OnUpdate;
            if (gameObject.activeSelf)
            {
                EventCenter.RemoveListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
            }
        }
    }
}

